package com.mipo;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.bson.Document;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.QueryOperators;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
public class Query {
	public static void main(String[] args) throws Exception {
		/*连接到MongoDB服务 如果是远程连接可以替换localhost为服务器所在IP地址
		两个参数分别服务器地和端口*/
		ServerAddress serverAddress = new ServerAddress("172.31.12.54", 22017);
		List<ServerAddress> addrs = new ArrayList<ServerAddress>();
		addrs.add(serverAddress);
		//三个参数分别用户名、数据库名、密码
		MongoCredential credential = MongoCredential.createMongoCRCredential("zsmongo", "third_swap_data", "zsmd@017".toCharArray());
		List<MongoCredential> credentials = new ArrayList<MongoCredential>();
		credentials.add(credential);
		//通过连接认证获取MongoDB连接
		MongoClient mongoClient = new MongoClient(addrs, credentials);
		MongoDatabase db = mongoClient.getDatabase("third_swap_data");
		MongoCollection<Document> users = db.getCollection("c006");
		getFirst(users);
		useCursor(users);
		operation(users);
		mongoClient.close();
	}
	/*取第一个*/
	public static void getFirst(MongoCollection<Document> users){
		FindIterable<Document> documents = users.find().skip(1).limit(1); // 从1开始取1条数据，下标从0开始
		System.out.println("queryAll:" + documents.first());
	}
	/*使用游标*/
    public static void useCursor(MongoCollection<Document> users){
    	MongoCursor<Document> cursor = users.find().skip(1).limit(10).iterator();
    	while(cursor.hasNext()) {
    		Document ducument = cursor.next();
    		System.out.println(ducument);
    	}
    }
    /*各种查询操作，文档如下：*/
    public static void operation(MongoCollection<Document> users){
    	/*查询_id = 4de73f7acd812d61b4626a77*/
    	FindIterable<Document> documents = null;
    	documents = users.find(new BasicDBObject("_id", "PD0003362467")); /*因为存的时候_id被存为字符串,所以第二个参数为字符串。如果是_id默认为ObjectId对象，则第二个参数应该为 new ObjectId("4de73f7acd812d61b4626a77")*/
    	System.out.println("1:" + documents.first());
    	/*查询downCount = 28*/
    	documents = users.find(new BasicDBObject("downCount", 28));
    	System.out.println("2:" + documents.first());
    	/*查询downCount >= 28*/
    	documents = users.find(new BasicDBObject("downCount", new BasicDBObject("$gte", 28)));
    	System.out.println("3:" + documents.first());
    	/*查询downCount <= 28*/
    	documents = users.find(new BasicDBObject("downCount", new BasicDBObject("$lte", 28)));
    	System.out.println("4:" + documents.first());
    	/*查询downCount != 28*/
    	documents = users.find(new BasicDBObject("downCount", new BasicDBObject("$ne", 28)));
    	System.out.println("5:" + documents.first());
    	/*查询downCount in 25/26/27*/
    	documents = users.find(new BasicDBObject("downCount", new BasicDBObject(QueryOperators.IN, new int[]{25, 26, 27})));
    	System.out.println("6:" + documents.first());
    	/*查询downCount not in 25/26/27*/
    	documents = users.find(new BasicDBObject("downCount", new BasicDBObject(QueryOperators.NIN, new int[]{25, 26, 27})));
    	System.out.println("7:" + documents.first());
    	/*查询updateResInfo字段存在的记录*/
    	documents = users.find(new BasicDBObject("updateResInfo.result.resourceId", new BasicDBObject(QueryOperators.EXISTS, true)));
    	System.out.println("8:" + documents.first());
    	/*模糊查询*/
    	BasicDBObject query=new BasicDBObject();
    	BasicDBList conditonList = new BasicDBList();
    	Pattern pattern = Pattern.compile("^.*code.*$", Pattern.CASE_INSENSITIVE);
    	BasicDBObject condition1 = new BasicDBObject();
    	condition1.put("updateResInfo.result.resourceId", pattern);
    	BasicDBObject condition2 = new BasicDBObject("downCount", new BasicDBObject(QueryOperators.NIN, new int[]{25, 26, 27}));
    	condition2.put("updateResInfo.result.resourceId", pattern);
    	conditonList.add(condition1);
    	conditonList.add(condition2);
    	query.put("$and", conditonList); // 对应的枚举值为：QueryOperators.OR或者QueryOperators.AND
    	documents = users.find(query);
    	System.out.println("9:" + documents.first());
    	/*in查询第二种写法*/
    	BasicDBObject query2 = new BasicDBObject();
    	BasicDBList cond = new BasicDBList();
    	cond.add(25);
    	cond.add(26);
    	cond.add(27);
    	query2.put("downCount",new BasicDBObject("$in", cond));
    	documents = users.find(query2);
    	System.out.println("10:" + documents.first());
    	//查询数组的数组
    	documents = users.find(new BasicDBObject("subjectIdList.0", "jcsub20"));
    	System.out.println("11:" + documents.first());
    }
}


